<template>
	<div>
		<div class="container">
			<div class="toolbar">
				<a-form ref="searchRef" layout="inline" :model="searchState" @finish="current=1;loading=true;initData()">
					<a-form-item label="合同名称">
						<a-input v-model:value="searchState.name" placeholder="请输入" allow-clear></a-input>
					</a-form-item>

					<a-form-item label="合同编号">
						<a-input v-model:value="searchState.code" placeholder="请输入" allow-clear></a-input>
					</a-form-item>

					<a-form-item>
						<a-space>
							<a-button type="primary" html-type="submit">
								<search-outlined />搜索
							</a-button>
						</a-space>
					</a-form-item>
				</a-form>
			</div>

			<div class="mt-2">
				<!--
				v-if="!loading"使得:default-expand-all-rows="true"生效
				-->
				<a-table v-if="!loading" :columns="columns" :scroll="tableScroll" row-key="id" children-column-name="children" :default-expand-all-rows="false" :data-source="contractSource" :pagination="pagination" :loading="loading" @change="initData">
					<template #bodyCell="{ column, record }">
						<template v-if="column.key === 'operation'">
							<a-space>
								<a-button v-if="record.price_updown === 1" type="link" @click="showPrice(record)">
									<account-book-outlined />价格维护
								</a-button>
							</a-space>
						</template>
					</template>
				</a-table>
			</div>
		</div>
	</div>
</template>

<script setup>
import { reactive, ref, computed } from "vue";
import { useRouter } from "vue-router";

import dayjs from "dayjs";

import { SearchOutlined, AccountBookOutlined } from "@ant-design/icons-vue";

import { post } from "../utils";

import { usePagination } from "vue-request";

const searchRef = ref();
const searchState = reactive({});

const columns = [
	{
		title: "ID",
		dataIndex: "id",
		key: "id",
		width: 120,
		align: "left",
		fixed: "left"
	},
	{
		title: "合同名称",
		dataIndex: "name",
		sorter: false,
		width: 300,
		ellipsis: true,
		fixed: "left"
	},
	{
		title: "合同编号",
		dataIndex: "code",
		sorter: false,
		width: 300,
		ellipsis: true
	},
	{
		title: "供应商",
		dataIndex: "supplier",
		width: 300,
		ellipsis: true,
		customRender: ({ text, record, index, column }) => {
			//未设置dataIndex，text和record返回值相同
			return text.company_name || text.fullname || text.username;
		}
	},
	{
		title: "合同金额",
		dataIndex: "amount",
		sorter: false,
		width: 120,
		align: "center"
	},
	{},
	{
		title: "最后更新时间",
		dataIndex: "update_time",
		width: 180,
		align: "center",
		customRender: ({ text, record, index, column }) => {
			return dayjs(text).format("YYYY-MM-DD HH:mm:ss");
		}
	},
	{
		title: "操作",
		key: "operation",
		sorter: false,
		fixed: "right",
		width: 300,
		align: "center"
	}
];
const tableScroll = computed(() => ({ x: document.documentElement.clientWidth - 650 }));

const { data, loading, run, current, pageSize, total } = usePagination(params => post("contract", { price_updown: 1, ...params, ...searchState }), {
	defaultParams: [
		{
			page_size: 10
		}
	],
	pagination: {
		currentKey: "page",
		pageSizeKey: "page_size",
		totalKey: "total"
	}
});

const contractSource = computed(() =>
	(data.value?.data || []).map(d => {
		if (d.children?.length === 0) delete d.children;
		return d;
	})
);

const pagination = computed(() => ({
	total: total.value,
	current: current.value,
	pageSize: pageSize.value
}));

const initData = (pag, filters, sorter) => {
	pag = Object.assign(
		{
			current: current.value,
			pageSize: pageSize.value
		},
		pag || {}
	);
	filters = { ...filters, ...searchState };

	run({
		page_size: pag?.pageSize,
		page: pag?.current,
		sort_field: sorter?.field,
		sort_order: sorter?.order,
		...filters
	});
};

const router = useRouter();
const showPrice = ({ id }) => {
	router.push(`/contract/price/${id}`);
};
</script>
<style scoped lang="scss">
.container {
	background: #fff;
	padding: 12px;
}
</style>